package plugins.fr.umlv.ged.mail;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.FileableCmisObject;

import fr.umlv.authentication.SessionHandler;

// TODO: Auto-generated Javadoc
/**
 * The Class SendMailSSL.
 */
public class SendMailSSL {

	/** The host. */
	private final String host = "smtp.gmail.com";
	
	/** The port. */
	private final String port = "465";
	
	/** The session. */
	private final Session session;
	
	/** The Constant username. */
	private static final String username = SessionHandler.getLogin();
	
	/** The file. */
	private CmisObject file;
	
	/** The permissions. */
	private HashMap<Person, List<String>> permissions;

	/** The Constant FROM. */
	private final static String FROM = "gedi.univ.paris.est@gmail.com";
	
	/** The Constant PASSWORD. */
	private final static String PASSWORD = "alfresco";

	/**
	 * Instantiates a new send mail ssl.
	 *
	 * @param permissions the permissions
	 * @param file the file
	 */
	public SendMailSSL(HashMap<Person, List<String>> permissions,CmisObject file) {
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.socketFactory.port", port);
		props.put("mail.smtp.socketFactory.class",
		"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", port);

		this.session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(FROM,PASSWORD);
			}
		});

		this.permissions=permissions;
		this.file=file;
	}

	/**
	 * Instantiates a new send mail ssl.
	 */
	public SendMailSSL() {
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.socketFactory.port", port);
		props.put("mail.smtp.socketFactory.class",
		"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", port);

		this.session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(FROM,PASSWORD);
			}
		});
	
	}


	/**
	 * Send mail invite.
	 */
	public void sendMailInvite(){
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(FROM));
			FileableCmisObject fileable = (FileableCmisObject)file;
			List<String> path = fileable.getPaths();
			message.setSubject("GED ++ Invitation");
			for (Map.Entry<Person,List<String>> e : permissions.entrySet()) {
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(e.getKey().getMail()));
				List<String> listPermissions = e.getValue();

				message.setText("Bonjour "+e.getKey().getUserName()+"," +
						"\n\n Les droits vous ont été accordés\n\n "+ listPermissions +"\n\npour accéder au contenu suivant\n\n"+file.getName()+" situé sous l'arborescence suivante "+path
						+"\n\n Cordialement, "+username);
				Transport.send(message);

			}
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Send mail plugin.
	 *
	 * @param user the user
	 * @param users the users
	 * @param subject the subject
	 * @param mess the mess
	 */
	public void sendMailPlugin(String user, String[] users,String subject, String mess){
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(FROM));
			message.setSubject(subject);
			message.setText(mess);
			if(!user.equals("")){
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(user));
				Transport.send(message);
			}
			for(int i=0;i<users.length;i++){
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(users[i]));
				Transport.send(message);
				System.out.println(users[i]);
			}

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * Send mail plugin.
	 *
	 * @param user the user
	 * @param subject the subject
	 * @param mess the mess
	 */
	public void sendMailPlugin(String user,String subject, String mess){
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(FROM));
			message.setSubject(subject);
			message.setText(mess);
			if(!user.equals("")){
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(user));
				Transport.send(message);
			}
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}
